package com.cookor.poi.modules.mx.util;

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

public class MD5Util {
    public static String md5(String text, String salt, String key) throws Exception {
        String md5str = DigestUtils.md5Hex(text + salt + key);
        return md5str;
    }

    public static String md52(String text) throws Exception {
        String md5str = DigestUtils.md5Hex(text);
        System.out.println("MD52加密后的字符串为:" + md5str + "\t长度：" + md5str.length());
        return md5str;
    }

    public static boolean verify(String text, String salt, String key, String md5) throws Exception {
        String md5str = md5(text, salt, key);
        System.out.println(md5str);
        if (md5str.equalsIgnoreCase(md5))
            return true;
        return false;
    }

    @Test
    public void test() throws Exception {
        String s = "userId=12fe33cd83df4b669f540458129c45c2;mobile=13055887894;userName=测试用户;";
        String kuz2p8m6ag = md5(s, "123456", "9wjofm8vzw0c0tz4i9z5h1cs");
        System.err.println(kuz2p8m6ag);
    }

    public static String encode(String str, String pwd) {
        byte[] result = null;
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(pwd.getBytes());
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(1, key);
            byte[] byteContent = str.getBytes();
            result = cipher.doFinal(byteContent);
        } catch (Exception e) {
            return null;
        }
        return parseByte2HexStr(result);
    }

    public static String decode(String str, String pwd) {
        byte[] result = null;
        byte[] content = parseHexStr2Byte(str);
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(pwd.getBytes());
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(2, key);
            result = cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return new String(result);
    }

    private static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1)
                hex = '0' + hex;
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main(String[] args) {
        String content = "code=12fe33cd83df4b669f540458129c45c2;mobile=13055887894;userName=测试用户;type=3;equityCode=EQ20230628144555;orderNo=428176240410655;";
        String pwd = "ae9b787c1dba463a";
        System.out.println("加密前：" + content);
        String encodeResultStr = encode(content, pwd);
        System.out.println("加密后：" + encodeResultStr);
        encodeResultStr = "XQaWznVa08taIh49zJDTnJzbBPFaOMKel4bYhIekLU7SI6x RuuUh7f27H5nbMGIpF7H6ELFDyDyMnV4FrY0bCI YjWhFHjdRXLnntTNUiE=".toLowerCase();
        System.out.println("encodeResultStr：" + encodeResultStr);
        pwd = "ae9b787c1dba463a";
        String decodeResultStr = decode(encodeResultStr, pwd);
        System.out.println("解密后：" + decodeResultStr);
    }
}
